package com.lehecai.ucenter.constant;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: qatang
 * Date: 13-10-14
 * Time: 11:51
 * To change this template use File | Settings | File Templates.
 */
public enum FormatType {

    FORMAT_TYPE_RAW(1, "raw", "raw"),
    FORMAT_TYPE_JSON(2, "json", "json"),
    FORMAT_TYPE_XML(3, "xml", "xml");

    private int code;
    private String key;
    private String name;

    private FormatType(int code, String key, String name){
        this.code = code;
        this.key = key;
        this.name = name;
    }

    public int getCode() {
        return code;
    }

    public String getKey() {
        return key;
    }

    public String getName() {
        return name;
    }

    private static Logger logger = LoggerFactory.getLogger(FormatType.class);
    private static final Map<String, FormatType> FORMAT_TYPE_KEY_MAP = new HashMap<String, FormatType>();
    static {
        for(FormatType formatType : FormatType.values()){
            FORMAT_TYPE_KEY_MAP.put(formatType.getKey(), formatType);
        }
    }

    public static FormatType getByKey(String key){
        try{
            return FORMAT_TYPE_KEY_MAP.get(key);
        }catch(Exception e){
            logger.error(e.getMessage(), e);
            return null;
        }
    }
}
